package com.ticketbooking.controller.FlightManager;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.ticketbooking.constant.Constant;
import com.ticketbooking.entity.RouteInfEntity;
import com.ticketbooking.service.IRouteInfDao;
import com.ticketbooking.util.SecurityTool;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;

@Controller
@RequestMapping("/flightmanager")
public class FlightManagerController {
    @Resource
    private IRouteInfDao routeInfDao;

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/addRouteInf")
    public String addRouteInf(HttpServletRequest request,Model model) {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String routeid = request.getParameter("routeid");
//        String routeid = new String(request.getParameter("routeid").getBytes("iso-8859-1"), "utf-8");
//        String str = new String(request.getParameter("参数名").getBytes("iso-8859-1"), "utf-8");
//        String departure= new String(request.getParameter("departure").getBytes("iso-8859-1"), "utf-8");
        String departure = request.getParameter("departure");
        String destination= request.getParameter("destination");
        String airline= request.getParameter("airline");
        String airtype= request.getParameter("airtype");
        String depairport= request.getParameter("depairport");
        String desairport= request.getParameter("desairport");
        String deptime= request.getParameter("deptime");
        String destime= request.getParameter("destime");
        String vipprice= request.getParameter("vipprice");
        String norprice= request.getParameter("norprice");
        String remarks= request.getParameter("remarks");
        RouteInfEntity routeinf = new RouteInfEntity();
        routeinf.setRouteid(routeid);
        routeinf.setRemarks(remarks);
        routeinf.setNorprice(norprice);
        routeinf.setDestime(destime);
        routeinf.setVipprice(vipprice);
        routeinf.setDeptime(deptime);
        routeinf.setDeparture(departure);
        routeinf.setDestination(destination);
        routeinf.setDepairport(depairport);
        routeinf.setDesairport(desairport);
        routeinf.setAirline(airline);
        routeinf.setAirtype(airtype);
        routeInfDao.save(routeinf);

        model.addAttribute("result","ok");
        return Constant.JSON_VIEW_OTHER;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getRouteInf")
    public String getRouteInfMsg(HttpServletRequest request, Model model){
        List routeInfList = null;
        if(SecurityTool.isReturnJson("2",request)){
            routeInfList = routeInfDao.getRouteInf();
        }
        model.addAttribute("routeInfList",routeInfList);
        return Constant.JSON_VIEW_OTHER;
    }


    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getRouteInfById")
    public String getRouteInfMsgId(HttpServletRequest request, Model model,String ID){
        List routeInfList = null;
        if(SecurityTool.isReturnJson("2",request)){
            routeInfList = routeInfDao.getRouteInfByRouteId(ID);
        }
        model.addAttribute("routeInfList",routeInfList);
        return Constant.JSON_VIEW_OTHER;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getRouteInfByDepTime")
    public String getRouteInfMsgTime(HttpServletRequest request, Model model,String begintime,String destime){
        List routeInfList = null;
        if(SecurityTool.isReturnJson("2",request)){
            routeInfList = routeInfDao.getRouteInfByDepTime(begintime,destime);
        }
        model.addAttribute("routeInfList",routeInfList);
        return Constant.JSON_VIEW_OTHER;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getRouteInfByPort")
    public String getRouteInfMsgPort(HttpServletRequest request, Model model,String deport,String desport){
        List routeInfList = null;
        if(SecurityTool.isReturnJson("2",request)){
            routeInfList = routeInfDao.getRouteInfByPort(deport,desport);
        }
        model.addAttribute("routeInfList",routeInfList);
        return Constant.JSON_VIEW_OTHER;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/getRouteInfPriceById")
    public String getRouteInfPriceId(HttpServletRequest request, Model model,String routeid){
        List routeInfList = null;
        if(SecurityTool.isReturnJson("2",request)){
            routeInfList = routeInfDao.getRouteInfByRouteId(routeid);
        }
       Map<String, Object> mymap  = getWeek(routeInfList);
        model.addAttribute("routeInfList",mymap);
        return Constant.JSON_VIEW_OTHER;
    }

    private Map<String,Object> getWeek(List routeInfList){
        if(routeInfList==null||routeInfList.size()==0){
            return null;
        }
        Map<String, Object> mymap = (Map<String, Object>) routeInfList.get(0);
        String dayNames[] = {"A","B","C","D","E","F","G"};
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(int i = 0; i<routeInfList.size();i++) {
            Map<String, Object> map = (Map<String, Object>) routeInfList.get(i);
            String date = (String) map.get("deptime");
            try {
                c.setTime(sdf.parse(date));
            } catch (ParseException ex) {

            }
            map.put("week", dayNames[c.get(Calendar.DAY_OF_WEEK) - 1]);
            if(map.get("week")=="A") {
                mymap.put("A1",map.get("vipprice"));
                mymap.put("A2",map.get("norprice"));}
            else if(map.get("week")=="B") {
                        mymap.put("B1",map.get("vipprice"));
                        mymap.put("B2",map.get("norprice"));
            }
            else if(map.get("week")=="C") {
                mymap.put("C1",map.get("vipprice"));
                mymap.put("C2",map.get("norprice"));
            }
            else if(map.get("week")=="D") {
                mymap.put("D1",map.get("vipprice"));
                mymap.put("D2",map.get("norprice"));
            }
            else if(map.get("week")=="E") {
                mymap.put("E1",map.get("vipprice"));
                mymap.put("E2",map.get("norprice"));
            }
            else if(map.get("week")=="F") {
                mymap.put("F1",map.get("vipprice"));
                mymap.put("F2",map.get("norprice"));
            }
            else if(map.get("week")=="G"){
                mymap.put("G1",map.get("vipprice"));
                mymap.put("G2",map.get("norprice"));
            }
        }
        mymap.remove("week");
        mymap.remove("vipprice");
        mymap.remove("norprice");
        return mymap;
    }

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping("/updateRouteInf")
    public String updateRouteInf(HttpServletRequest request,Model model) {
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String routeid = request.getParameter("routeid");
//        String routeid = new String(request.getParameter("routeid").getBytes("iso-8859-1"), "utf-8");
//        String str = new String(request.getParameter("参数名").getBytes("iso-8859-1"), "utf-8");
//        String departure= new String(request.getParameter("departure").getBytes("iso-8859-1"), "utf-8");
        String departure = request.getParameter("departure");
        String destination= request.getParameter("destination");
        String airline= request.getParameter("airline");
        String airtype= request.getParameter("airtype");
        String depairport= request.getParameter("depairport");
        String desairport= request.getParameter("desairport");
        String deptime= request.getParameter("deptime");
        String destime= request.getParameter("destime");
        String vipprice= request.getParameter("vipprice");
        String norprice= request.getParameter("norprice");
        String remarks= request.getParameter("remarks");
        RouteInfEntity routeinf = new RouteInfEntity();
        routeinf.setRouteid(routeid);
        routeinf.setRemarks(remarks);
        routeinf.setNorprice(norprice);
        routeinf.setDestime(destime);
        routeinf.setVipprice(vipprice);
        routeinf.setDeptime(deptime);
        routeinf.setDeparture(departure);
        routeinf.setDestination(destination);
        routeinf.setDepairport(depairport);
        routeinf.setDesairport(desairport);
        routeinf.setAirline(airline);
        routeinf.setAirtype(airtype);
        routeInfDao.UpdateRouteInf(routeinf);
        model.addAttribute("result","ok");
        return Constant.JSON_VIEW_OTHER;
    }


}
